package com.self.campussecondhandmarket.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Date;
import java.util.Map;

/**
 * Author: Vincent
 * <p> DateTime: 2024/8/5 16:23
 * <p> Description: jwt工具类
 * <p>
 **/
@Slf4j
public class JWTUtil {
	
	private static final Key key = Keys.hmacShaKeyFor("cptbtptp,bcptdtptp.ksdjhafkhjsdfahjflWGEUYIROLBZVRLWUI.".getBytes(StandardCharsets.UTF_8));
	
	/**
	 * 生成token
	 *
	 * @param claims 元数据
	 * @return token
	 */
	public static String generateToken(Map<String, Object> claims) {
		
		return Jwts.builder()
				.claims(claims)
				.signWith(key)
				.expiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24))
				.compact();
	}
	
	
	/**
	 * 解析token
	 *
	 * @param token token
	 * @return 元数据
	 */
	public static Claims parseToken(String token) {
		return Jwts.parser()
				.setSigningKey(key)
				.build()
				.parseClaimsJws(token)
				.getBody();
	}
	
	
}
